import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import '../services/user.dart';
import 'package:dio/dio.dart';
import '../services/event_bus.dart';
import 'check_out/bottom.dart';
import 'check_out/check_out_item.dart';
import 'check_out/info.dart';
import 'check_out/pay_way.dart';
import 'check_out/address.dart';
class CheckOutPage extends StatefulWidget {
  CheckOutPage({Key key}) : super(key: key);

  _CheckOutPageState createState() => _CheckOutPageState();
}

class _CheckOutPageState extends State<CheckOutPage> {
  List _addressList = [];

  @override
  void initState() {
    super.initState();
    this._getDefaultAddress();

    //监听广播
    eventBus.on<CheckOutEvent>().listen((event) {
      print(event.str);
      this._getDefaultAddress();
    });
  }

  _getDefaultAddress() async {
    var api = 'api/oneAddressList';
    var response = await Dio().get(api);
    print(response);
    setState(() {
      this._addressList = response.data['result'];
    });
  }

  //底部弹出框
  _addressBottomSheet() {
    showModalBottomSheet(
        enableDrag: false,
        isScrollControlled:true,
        shape:RoundedRectangleBorder(
            borderRadius:BorderRadius.only(
              topLeft: Radius.circular(20),
              topRight: Radius.circular(20),
            )
        ),
        context: context,
        builder: (context) {
          return Address();
        });
  }


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: BackButton(
          color: Colors.white,
        ),
        title: Text(
          "提交订单",
            style: TextStyle(
        color: Colors.white,
            fontSize: 30.sp,
            fontWeight: FontWeight.bold),
        ),
        centerTitle: true,
        backgroundColor: Colors.red,
      ),
      body: Column(
        children: [
          Expanded(
            child: CustomScrollView(
              physics: BouncingScrollPhysics(),
              slivers: <Widget>[
                SliverToBoxAdapter(
                  child: Container(
                    decoration: BoxDecoration(
                        gradient: LinearGradient(
                            begin: Alignment.topCenter,
                            end: Alignment.bottomCenter,
                            colors: [Colors.red, Colors.white])),
                    child: Column(
                      children: <Widget>[
                        SizedBox(height: 10),
                        this._addressList.length > 0
                            ? ListTile(
                                title: Column(
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children: <Widget>[
                                    Text(
                                        "${this._addressList[0]["name"]}  ${this._addressList[0]["phone"]}"),
                                    SizedBox(height: 10),
                                    Text("${this._addressList[0]["address"]}"),
                                  ],
                                ),
                                trailing: Icon(Icons.navigate_next),
                                onTap: () {
                                  Navigator.of(context)
                                      .pushNamed('/addressList');
                                },
                              )
                            : Container(
                                width: 700.w,
                                decoration: BoxDecoration(
                                    color: Colors.white,
                                    borderRadius: BorderRadius.all(
                                      Radius.circular(20.w),
                                    )),
                                child: ListTile(
                                  leading: Text("设置收货地址"),
                                  trailing: Icon(Icons.navigate_next),
                                  onTap: () {
                                    _addressBottomSheet();
                                  },
                                ),
                              ),
                        SizedBox(height: 10),
                      ],
                    ),
                  ),
                ),
                SliverToBoxAdapter(
                  child: SizedBox(height: 20),
                ),
                SliverToBoxAdapter(
                  child: Container(
                    color: Colors.white,
                    height: 100.w,
                    padding:
                        EdgeInsets.symmetric(horizontal: 25.w, vertical: 25.w),
                    child: Text('共2件商品'),
                  ),
                ),
                SliverToBoxAdapter(
                  child: Container(
                    color: Colors.white,
                    padding: EdgeInsets.all(20.w),
                    child: Column(children: [
                      Column(
                        children: <Widget>[
                          CheckOutItem(),
                          Divider(),
                          CheckOutItem(),
                          Divider(),
                        ],
                      )
                    ]),
                  ),
                ),
                SliverToBoxAdapter(
                  child: SizedBox(height: 20),
                ),
                SliverToBoxAdapter(
                  child: Info(),
                ),
                SliverToBoxAdapter(
                  child: PayWay(),
                ),
                SliverToBoxAdapter(
                  child: Container(
                    height:75.w,
                    margin: EdgeInsets.only(bottom: 50.w),
                    padding: EdgeInsets.all(20.w),
                    color: Colors.white,
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Text('商品总价：'),
                        Text('￥300.00',style: TextStyle(
                            color: Colors.grey[600]
                        ),)
                      ],
                    ),
                  ),
                ),
              ],
            ),
          ),
          Bottom()
        ],
      ),
    );
  }
}
